package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;
import com.younion.common.util.ImageFileUtil;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.IAppInitService;
import com.younion.vo.AppInitVO;

/**
 * @author wells
*/
public class AppInitAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(AppInitAction.class);

	private IAppInitService appInitService = null;

	private AppInitVO appInitVO = new AppInitVO();

	private List<AppInitVO> appInitList;
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listAppInit() {
		try {
			Map paramMap = null;
			if (appInitVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(appInitVO));
			}
			// 获取总记录数 
			int total = appInitService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象 
			Paginator paginator = this.getPaginator(total);
			appInitList = appInitService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 *  新增
	 */
	public String addAppInit() {
		return SUCCESS;
	}

	/**
	 *  修改保存，调度Jsp
	 */
	public String editAppInit() {
		try {
			appInitVO = appInitService.getById(appInitVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 *  保存动作
	 */
	public String saveAppInit() {
		try {
			if(appInitVO.getPicid()==null){
				appInitVO.setPicid(0);
			}else{
				appInitVO.setPicid(appInitVO.getPicid()+1);
			}
			int id = appInitService.saveReturnKey(appInitVO);
			ImageFileUtil.processImageFiles(id, "1", request);
			this.addLog(4, "修改app启动图片", "修改app启动图片",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 *  删除
	 */
	public String deleteAppInit() {
		try {
			String pkId = request.getParameter("key");
			appInitService.remove(Arrays.asList(pkId.split(",")));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 *  查看
	 */
	public String viewAppInit() {
		try {
			String pkId = request.getParameter("key"); 
			appInitVO = appInitService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public AppInitVO getAppInitVO() {
		return appInitVO;
	}

	public void setAppInitVO(AppInitVO appInitVO) {
		this.appInitVO = appInitVO;
	}

	public void setAppInitService(IAppInitService appInitService) {
		this.appInitService = appInitService;
	}

	public List<AppInitVO> getAppInitList() {
		return appInitList;
	}

	public void setAppInitList(List<AppInitVO> appInitList) {
		this.appInitList = appInitList;
	}

}